package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lqw);
        if (emp == null) {
            return R.error("用户不存在");
        }
        if (!password.equals(emp.getPassword())) {
            return R.error("密码错误");
        }
        if (emp.getStatus() == 0) {
            return R.error("该用户已被禁用");
        }
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    @PostMapping("logout")
    public R<Employee> logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return R.success(null);
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee emp) {
        //初始化创建时间、初始化创建人、初始化密码
        //Long empId = (Long) request.getSession().getAttribute("employee");
        /*emp.setCreateUser(empId);
        emp.setUpdateUser(empId);*/
        /*emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());*/
        emp.setPassword(DigestUtils.md5DigestAsHex("000000".getBytes()));
        employeeService.save(emp);
        return R.success("用户创建成功！");//页面不提示该条信息
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> SelectPage(int page, int pageSize, String name) {
        //分页条件
        Page<Employee> pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        //查询条件
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        lqw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, lqw);
        return R.success(pageInfo);
    }
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee){
        Long userId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(userId);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee emp = employeeService.getById(id);
        if(emp!=null){
            return R.success(emp);
        }
        return R.error("未查询到对应员工信息");
    }
}

